package com.example.springboottools.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class HttpClientBenSenUtils {
    static String recUrl = "ttps://openapi.italent.cn/";
    static String recGrantType = "client_credentials";

    static String recAppKey = "CE3684B983B44730AF0E82DF1B74041F";
    static String recAppSecret = "740A5AC6C0134A81B0567DBF6B90000A35F2B8F40AEF49B5BB89C470605D4631";
    public static JSONObject getEmployee(Map<String, Object> parameters){

        String httpUrl=recUrl+"TenantBaseExternal/api/v5/Employee/GetByTimeWindow";
        return getJsonObject(parameters, httpUrl);
    }

    private static JSONObject getJsonObject(Map<String, Object> parameters, String httpUrl) {
        String token = getToken();
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer " + token);
        String resResult = HttpClientHRUtils.sendPostWithHeadr(httpUrl, JSON.toJSONString(parameters),headers);
        return JSONObject.parseObject(resResult);
    }
    public static String getToken(){
        String httpUrl=recUrl+"token";
        Map<String, Object> parameters=new HashMap<>();
        parameters.put("grant_type",recGrantType);
        parameters.put("app_key",recAppKey);
        parameters.put("app_secret",recAppSecret);
        String resResult = HttpClientUtils.httpPostStr(httpUrl, null, parameters, Boolean.TRUE);
        JSONObject resObject = JSONObject.parseObject(resResult);
        return resObject.getString("access_token");
    }
}
